package com.gobo.jinghuimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.common.utils.PageUtils;
import com.gobo.common.utils.Query;

import com.gobo.jinghuimall.product.dao.ProductAttrValueDao;
import com.gobo.jinghuimall.product.entity.ProductAttrValueEntity;
import com.gobo.jinghuimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProductAttrValueEntity> listforSpu(Long spuId) {

        QueryWrapper<ProductAttrValueEntity> spu_id = new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId);
        List<ProductAttrValueEntity> productAttrValueEntities = this.baseMapper.selectList(spu_id);
        return productAttrValueEntities;


    }

    @Override
    @Transactional
    public void updateAttrSpu(Long spuId, List<ProductAttrValueEntity> productAttrValueEntities) {
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        List<ProductAttrValueEntity> collect = productAttrValueEntities.stream().map(p -> {
            p.setSpuId(spuId);
            p.setAttrSort(1);
            return p;
        }).collect(Collectors.toList());
        this.saveBatch(collect);

    }

    /**
     * 返回所有可以通过spuId关联的属性(且可以被检索的)
     * @param spuId
     * @return
     */
    @Override
    public List<ProductAttrValueEntity> baseAttrListforspu(Long spuId) {

        List<ProductAttrValueEntity> productAttrValueEntities = baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return productAttrValueEntities;

    }

    @Override
    public List<String> getSaleAttr(Long skuId) {
        return baseMapper.getSaleAttr(skuId);
    }

}